package com.dllo.lanou.cookapp.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.HeadViewPageResultEntity;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;

import java.util.List;

/**
 * Created by xijinhua on 2016/3/5.
 */
public class HomeListFoodAdapter extends BaseAdapter {
    private List<HeadViewPageResultEntity> resultEntities;
    private LayoutInflater inflater;
    public HomeListFoodAdapter(Context context){
        this.inflater = LayoutInflater.from(context);
    }
    public void addData(List<HeadViewPageResultEntity> resultEntities){
        this.resultEntities = resultEntities;

        notifyDataSetChanged();
        Log.i("w我的菜谱",resultEntities+"");
    }


    @Override
    public int getCount() {
        return resultEntities!=null && resultEntities.size()>0?resultEntities.size():0;
    }

    @Override
    public Object getItem(int position) {
        return resultEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder;
        if (convertView == null){
           convertView = inflater.inflate(R.layout.item_homelistfood,null);
           holder = new ViewHolder();
            holder.img= (ImageView) convertView.findViewById(R.id.item_homelist_img);
            holder.des = (TextView) convertView.findViewById(R.id.item_homelist_des);
            holder.name = (TextView) convertView.findViewById(R.id.item_homelist_name);
            convertView.setTag(holder);
        }
        else {
            holder= (ViewHolder) convertView.getTag();
        }
        holder.des.setText(resultEntities.get(position).getDescription());
        holder.name.setText(resultEntities.get(position).getName());

        String url = "http://tnfs.tngou.net/image/"+resultEntities.get(position).getImg();
        GetVolleyImage.getNetWorkImg(url,holder.img);
        return convertView;
    }

    public static class ViewHolder{
        public ImageView img;
        public TextView name;
        public TextView des;
    }
}
